package com.zjw.video.custom.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjw.video.common.entity.VideoEntity;
import com.zjw.video.common.entity.VideoTagEntity;
import com.zjw.video.common.entity.VideoTopEntity;
import com.zjw.video.common.entity.req.TagVideoListReqVO;
import com.zjw.video.common.entity.resp.BaseResp;
import com.zjw.video.common.entity.resp.TagRespVO;
import com.zjw.video.common.entity.resp.VideoRespVO;
import com.zjw.video.common.mapper.VideoMapper;
import com.zjw.video.custom.service.VideoService;
import com.zjw.video.custom.service.VideoTagService;
import com.zjw.video.custom.service.VideoTopService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, VideoEntity> implements VideoService {

    @Resource
    private VideoTopService videoTopService;
    @Resource
    private VideoTagService videoTagService;

    @Override
    public BaseResp<List<VideoRespVO>> videoTopList() {
        List<VideoRespVO> videoRespVOS = new ArrayList<>();
        List<VideoTopEntity> list = videoTopService.lambdaQuery()
                .orderByAsc(VideoTopEntity::getSort)
                .list();
        List<Long> videoIds = list.stream().map(VideoTopEntity::getVideoId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(videoIds)) {
            return BaseResp.success();
        }
        List<VideoEntity> videoEntities = getBaseMapper().selectBatchIds(videoIds);
        for (VideoEntity videoEntity : videoEntities) {
            VideoRespVO videoRespVO = new VideoRespVO();
            BeanUtils.copyProperties(videoEntity, videoRespVO);
            videoRespVOS.add(videoRespVO);
        }
        return BaseResp.success(videoRespVOS);
    }

    @Override
    public Page<VideoRespVO> tagVideoList(TagVideoListReqVO tagVideoListReqVO) {
        List<VideoTagEntity> list = videoTagService.lambdaQuery()
                .eq(VideoTagEntity::getTagId, tagVideoListReqVO.getTagId())
                .list();
        List<Long> ids = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            ids = list.stream().map(VideoTagEntity::getVideoId).collect(Collectors.toList());
        }
        Page<VideoEntity> videoEntityPage = lambdaQuery()
                .in(!CollectionUtils.isEmpty(ids), VideoEntity::getId, ids)
                .in(!CollectionUtils.isEmpty(tagVideoListReqVO.getOrigins()), VideoEntity::getOrigin, tagVideoListReqVO.getOrigins())
                .in(!CollectionUtils.isEmpty(tagVideoListReqVO.getTypes()), VideoEntity::getType, tagVideoListReqVO.getTypes())
                .like(StringUtils.isNotBlank(tagVideoListReqVO.getName()), VideoEntity::getName, tagVideoListReqVO.getName())
                .ge(!Objects.isNull(tagVideoListReqVO.getPublishTimeStart()), VideoEntity::getPublishTime, tagVideoListReqVO.getPublishTimeStart())
                .le(!Objects.isNull(tagVideoListReqVO.getPublishTimeEnd()), VideoEntity::getPublishTime, tagVideoListReqVO.getPublishTimeEnd())
                .page(new Page<>(tagVideoListReqVO.getPageNum(), tagVideoListReqVO.getPageSize()));
        Page<VideoRespVO> videoRespVOPage = new Page<>();
        videoRespVOPage.setCurrent(videoEntityPage.getCurrent());
        videoRespVOPage.setPages(videoEntityPage.getPages());
        videoRespVOPage.setSize(videoEntityPage.getSize());
        videoRespVOPage.setTotal(videoEntityPage.getTotal());
        List<VideoRespVO> videoRespVOS = new ArrayList<>();
        for (VideoEntity record : videoEntityPage.getRecords()) {
            VideoRespVO videoRespVO = new VideoRespVO();
            BeanUtils.copyProperties(record, videoRespVO);
            videoRespVOS.add(videoRespVO);
        }
        videoRespVOPage.setRecords(videoRespVOS);
        return videoRespVOPage;
    }

    @Override
    public VideoRespVO videoDetail(Long id) {
        VideoEntity videoEntity = getById(id);
        VideoRespVO videoRespVO = new VideoRespVO();
        BeanUtils.copyProperties(videoEntity, videoRespVO);
        return videoRespVO;
    }
}
